// 权限控制
import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 定义白名单
const whiteList = ['/login', '/404']

// 路由前置守卫
router.beforeEach((to, from, next) => {
  // 在页面进入的时候就开启进度条
  NProgress.start()
  if (store.state.user.token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      if (!store.state.user.userInfo.userId) {
        store.dispatch('user/fetchUserInfo')
      }
      next()
    }
    // to.path === '/login' ? next('/') : next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
    // whiteList.includes(to.path) ? next() : next('/login')
    NProgress.done()
  }
})

// 路由后置守卫
router.afterEach((to, from) => {
  // 页面进入完成之后关闭进度条
  NProgress.done()
})
